<?php

namespace Admin\Controller;

class CategoryController extends CommonController{

    public function index(){
        $this->display('list');
    }
    public function add(){
        $this->display();
    }
    public function edit(){
        $this->display();
    }
    public function gets(){
        $category_id=I('get.id');
        $categoryModel = M('Category');
        $where=[];
        $is_tree=false;
        if(isset($category_id)&&!empty($category_id)){
            $where['id']=['eq',$category_id];
            $is_tree=true;
        }

        $ret       = $categoryModel->where($where)->order('id desc')->select();
        array_walk($ret, function (&$v) {
            $v['createtime'] = date('Y/m/d h:i:s');
        });
      
        if(!$is_tree){
            $ret = list_to_tree($ret);
        }
        $this->ajaxReturn(['status' => true, 'msg' =>$ret ]);
    }
    public function get(){
        $id=I('get.id');
        if(!isset($id)||empty($id)){
            $this->ajaxReturn(['status' => true, 'msg' =>'参数错误' ]);
        }
        $categoryModel = M('Category');
        $ret       = $categoryModel->where("id={$id}")->find();

        $this->ajaxReturn(['status' => true, 'msg' =>$ret ]);
    }
    public function save(){
        if(IS_POST){
            $data=I('post.');
            if(empty($data['name'])){
                return $this->ajaxReturn(['status'=>0,'msg'=>'分类名称不能为空']);
            }if(strlen($data['name'])>80){
                return $this->ajaxReturn(['status'=>0,'msg'=>'分类名称过长！']);
            }
            if(empty($data['name'])){
                return $this->ajaxReturn(['status'=>0,'msg'=>'分类介绍不能为空！']);
            }
            $category_model=M('Category');
            $status=$category_model->data($data)->add();
            if($status){
                return $this->ajaxReturn(['status'=>1,'msg'=>'分类添加成功','url'=>U('category/index')]);
            }else{
                return $this->ajaxReturn(['status'=>0,'msg'=>'数据添加失败请重试！']);
            }
        }

    }

    public function del(){
        $id=I('get.id');
        if(empty($id)){
            $this->error('参数错误！');
        }
        $category_model=M('Category');

        $si_on=$category_model->where("pid={$id}")->find();
        if($si_on){
            return $this->ajaxReturn(['status'=>true,'msg'=>'不能删除，请先删除子类！']);
        }

        $ret=$category_model->where("id={$id}")->delete();

        if($ret){
            return $this->ajaxReturn(['status'=>true,'msg'=>'删除成功！']);
        }else{
            return $this->ajaxReturn(['status'=>false,'msg'=>'删除失败！']);
        }
    }

    public function update(){
        if(IS_POST){
            $data=I('post.');
            if(empty($data['name'])){
                return $this->ajaxReturn(['status'=>0,'msg'=>'分类名称不能为空']);
            }if(strlen($data['name'])>80){
                return $this->ajaxReturn(['status'=>0,'msg'=>'分类名称过长！']);
            }
            if(empty($data['name'])){
                return $this->ajaxReturn(['status'=>0,'msg'=>'分类介绍不能为空！']);
            }
            $category_model=M('Category');
            $status=$category_model->where("id={$data['id']}")->save($data);
            if($status){
                return $this->ajaxReturn(['status'=>1,'msg'=>'分类修改成功']);
            }else{
                return $this->ajaxReturn(['status'=>0,'msg'=>'数据修改失败请重试！']);
            }
        }
    }

}